package com.cat.dataStructure02;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/maximize-win-from-two-segments/description/
 * @create 2025/10/22 16:06
 * @since JDK17
 */

public class Solution42 {
    public int maximizeWin(int[] nums, int k) {
        int n = nums.length, cnt = 0, m1 = 0, m2 = 0;
        int[] arr = new int[n];
        for (int l = 0, r = 0; r < n; ) {
            int x = nums[l];
            while (r < n && nums[r] <= x + k) {
                r++;
            }
            arr[cnt++] = r - l;
            l = r;
        }
        for (int i = 0; i < cnt; i++) {
            if (m1 < arr[i]) {
                m2 = m1;
                m1 = arr[i];
            } else if (m2 < arr[i]) {
                m2 = arr[i];
            }
        }

        return m1 + m2;
    }
}
